package com.ptu.controller.admin;

import com.ptu.dto.UserDTO;
import com.ptu.entity.User;
import com.ptu.server.OSSService;
import com.ptu.server.UserServer;
import com.ptu.util.PageResult;
import com.ptu.util.Result;
import com.ptu.util.UserHolder;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController("adminUserController")
@RequestMapping("/admin/user")
@Tag(name = "管理员用户模块")
public class UserController {

    @Autowired
    UserServer userServer;

    @Autowired
    private OSSService ossService;

    @Operation(summary = "用户列表")
    @GetMapping("/list")
    public Result<PageResult> list(String phone, String userName,  Integer pageNum, Integer pageSize) {
        System.out.println("分页查询用户列表");
        PageResult  pageResult = userServer.getUserList(phone, userName, pageNum, pageSize);
        return Result.success(pageResult);
    }

    @Operation(summary = "用户详情")
    @GetMapping("/detail")
    public Result<User> detail(Integer id) {
        System.out.println("用户详情");
        User user = userServer.adminGetUser(id);
        return Result.success(user);
    }

    @Operation(summary = "修改用户信息")
    @PostMapping("/update")
    public Result update(UserDTO user) {
        System.out.println("修改用户信息");
        userServer.adminUpdate(user);
        return Result.success();
    }

    @Operation(summary = "删除用户")
    @DeleteMapping("/delete")
    public Result delete(Integer id) {
        System.out.println("删除用户");
        userServer.delete(id);
        return Result.success();
    }

    @Operation(summary = "新增用户")
    @PostMapping("/add")
    public Result add(UserDTO user) {
        System.out.println("新增用户");
        userServer.adminAdd(user);
        return Result.success();
    }

    @Operation(summary = "注册上传头像")
    @PostMapping("/upload/avatar")
    public Result<String> uploadAvatarRegister(@RequestParam("file") MultipartFile file) {
        System.out.println("注册上传头像");
        try {
            // 验证文件类型
            String contentType = file.getContentType();
            if (contentType == null || !contentType.startsWith("image/")) {
                return Result.error("请上传图片文件");
            }

            // 验证文件大小（2MB）
            if (file.getSize() > 2 * 1024 * 1024) {
                return Result.error("图片大小不能超过2MB");
            }

            // 获取用户当前头像URL
            UserDTO currentUser = UserHolder.getUser();
            String oldAvatarUrl = currentUser != null ? currentUser.getImg() : null;

            // 上传到OSS，同时删除旧头像
            String imageUrl = ossService.uploadFile(file, oldAvatarUrl);

            return Result.success(imageUrl);
        } catch (IOException e) {
            System.out.println("上传失败" + e);
            return Result.error("上传失败");
        }
    }

    @Operation(summary = "管理员登录")
    @PostMapping("/login")
    public Result login(String adminName, String password) {
        System.out.println("管理员登录");
        return Result.success(userServer.adminLogin(adminName, password));
    }
}
